import {Injectable} from '@angular/core';
import {HttpClient} from '@angular/common/http';

import {Observable} from "rxjs/Observable";
import 'rxjs/add/operator/map';

import {CustomerModel} from "../../model/customer/customer.model";

@Injectable()
export class MapProvider {
    constructor(public http: HttpClient) {
    }

    /**
     * 获取客户的头像(地图定位用)
     * @param {CustomerModel} customer
     * @returns {any}
     */
    static getCustomerAvatar(customer: CustomerModel) {
        if (!customer) return './assets/images/map/icon_unknown_map.png';
        if (customer.avatar) return customer.avatar;

        let gender = customer.sex;
        if (gender === 'M') {
            return './assets/images/map/icon_man_map.png';
        } else if (gender === 'F') {
            return './assets/images/map/icon_woman_map.png';
        } else {
            return './assets/images/map/icon_unknown_map.png';
        }
    }

    /**
     * 获取客户的头像(卡片详情用)
     * @param {CustomerModel} customer
     * @returns {string}
     */
    static getCustomerAvatarDetail(customer: CustomerModel) {
        if (!customer) return './assets/images/map/icon_map_unknown.png';
        if (customer.avatar) return customer.avatar;

        let gender = customer.sex;
        if (gender === 'M') {
            return './assets/images/map/icon_map_man.png';
        } else if (gender === 'F') {
            return './assets/images/map/icon_map_woman.png';
        } else {
            return './assets/images/map/icon_map_unknown.png';
        }
    }

    /**
     * 获取指定位置附近的客户数据
     * @param point
     * @param {number} distance
     * @returns {Observable<any>}
     */
    getNearCustomers(point, distance: number = 3000): Observable<any> {
        let url = `/lbs/v1/mapClientList?lat=${point.lat}&lng=${point.lng}&range=${distance}`;

        let mockCustomer = {
            amount: 20,
            data: [{
                lng: 121.5754325305202,
                lat: 31.212415385435087,
                clientInfo: [{
                    clientCode: '4303118557',
                    name: '徐19817102',
                    sex: 'F',
                    userType: 2
                }, {
                    clientCode: '4303138499',
                    name: '徐19817102',
                    sex: 'F',
                    userType: 2
                },
                    {
                        clientCode: '4300022153',
                        name: '徐19817102',
                        sex: 'F',
                        userType: 2
                    },
                    {
                        clientCode: '4301540785',
                        name: '徐19817102',
                        sex: 'F',
                        userType: 2
                    },
                ]
            }, {
                lng: 121.5754325305202,
                lat: 31.332415385435087,
                clientInfo: [{
                    clientCode: '4303118557',
                    name: '徐19817102',
                    sex: 'F',
                    userType: 2
                }]
            }]
        };

        return this.http.get(url).map((item) => {
            return item;
        })
    }

    /**
     * userType=1 准客户
     * userType=2 核心客户
     * @param {string} clientCode
     * @param {string} userType
     * @returns {Observable<any>}
     */
    getCustomerDetail(clientCode: string, userType: number) {
        if (userType == 1) {
            return this.http.get(`/leads/v1/lead/${clientCode}`).map((detail) => {
                return detail;
            });
        } else if (userType == 2) {
            return this.http.get(`/party/v1/detail/${clientCode}`).map((detail) => {
                return detail;
            });
        }
    }

    /**
     * 获取准客户列表
     * @param clientIDs  核心客户id
     * @returns {Observable<any>}
     */
    getCustomerListByType1(clientIDs: any): Observable<any> {
        const url = '/leads/v1/list/leads';
        return this.http.post(url, clientIDs).map(r => {
            return r;
        });
    }

    /**
     * 获取核心客户列表
     * @param clientIDs
     * @returns {Observable<any>}
     */
    getCustomerListByType2(clientIDs: any): Observable<any> {
        const url = '/party/v1/mapClientList';
        return this.http.post(url, clientIDs).map(r => {
            return r;
        });
    }

    /**
     * 位置检测
     * @param jsonArray
     * @returns {Observable<any>}
     */
    detectLocation(jsonArray: any): Observable<any> {
        const url = '/party/v1/detectLocation';
        return this.http.post(url, jsonArray).map(r => {
            return r;
        });
    }

    //客户搜索范围，初始化3km
    static readonly customerDistance = 3000;

}
